home *** CD-ROM | disk | FTP | other *** search
/ Programmer Power Tools / Programmer Power Tools.iso / progjrn / pj_6_3.arc / TIMER1 < prev    next >
Text File  |  1988-04-05  |  2KB  |  80 lines

  1. Code from Redirections, "Pink Noise" by Frank D. Greco.
  2. Copyright 1988 by Frank D. Greco.  No commercial use of this code
  3. without express permission of the author.
  4.  
  5. /*
  6.  * TIMER.H    --  Header file for timing programs
  7.  *
  8.  * When including and compiling with your programs:
  9.  *
  10.  * System V
  11.  *        $ cc -DSYSV program.c ... other files ...
  12.  *
  13.  * Berkeley-based Unix
  14.  *        % cc -DBSD program.c ... other files ...
  15.  *
  16.  * PC and Microsoft C [timing granularity 1/18 second]
  17.  *        C:\> cl -DPC program.c ... other files ...
  18.  *
  19.  * PC and Microsoft C [timing granularity 1/1000 second]
  20.  *        C:\> cl -DPC -DFAST_CLOCK program.c ... other files ...
  21.  *
  22.  */
  23.  
  24. #define DEBUG                    /* Turn on timing statistics */
  25.  
  26. #define TIX_DIFF            (_t2 - _t1)
  27.  
  28. /***************** For Berkeley Versions of Unix ****************/
  29.  
  30. #ifdef BSD
  31. #    include <sys/types.h>
  32. #    include <sys/time.h>
  33.     static double _t1, _t2;
  34.     static struct timeval _tv_;
  35.     static struct timeval _timez;
  36. #    define gettime()        gettimeofday(&_tv_, &_timez_)
  37. #    define START()            (gettime(), _t1 = _tv_.tv_usec/1e6 + _tv_.tv_sec)
  38. #    define STOP()            (gettime(), _t2 = _tv_.tv_usec/1e6 + _tv_.tv_sec)
  39. #    define PRINT_TIME(cp)    fprintf(stderr,"%s: %lf secs.\n",cp,TIX_DIFF)
  40. #endif
  41.  
  42. /***************** For System V Versions of Unix ****************/
  43.  
  44. #ifdef SYSV
  45. #    include <sys/types.h>
  46. #    include <sys/times.h>
  47.     static struct tms _currtime;
  48.     long times();
  49.     static long _t1, _t2;
  50. #    define gettime()        times(&_currtime)
  51. #    define TIX_PER_SEC        60.0            /* Verify for Your Machine */
  52.                                             /* For the 3B2 use 100.0 */
  53. #    define START()            _t1 = gettime()
  54. #    define STOP()            _t2 = gettime()
  55. #    define PRINT_TIME(cp)    fprintf(stderr,"%s: %lf secs.\n", \
  56.                                     cp, TIX_DIFF / TIX_PER_SEC)
  57. #endif
  58.  
  59. /***************** For the PC ****************/
  60.  
  61. #ifdef PC
  62. #    ifdef FAST_CLOCK
  63. #        define TIX_PER_SEC            1000.0
  64. #    else
  65. #        define TIX_PER_SEC            18.20648
  66. #    endif
  67.     unsigned long gettime();
  68.     static long _t1, _t2;        /* Tick values for calc. elapsed time */
  69. #    define START()            _t1 = gettime()
  70. #    define STOP()            _t2 = gettime()
  71. #    define PRINT_TIME(cp)    fprintf(stderr,"%s: %lf secs.\n", \
  72.                                     cp, TIX_DIFF / TIX_PER_SEC)
  73. #endif
  74.  
  75. #ifdef DEBUG
  76. #    define TIME(code, string)    START(); code; STOP(); PRINT_TIME(string)
  77. #else
  78. #    define TIME(code, string)    code
  79. #endif
  80.